package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zhidao.dao.FansMapper;
import com.example.zhidao.model.po.Fans;
import com.example.zhidao.model.po.User;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.model.vo.FansUser;
import com.example.zhidao.service.FansService;
import com.example.zhidao.utils.Ujwt;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/fans")
public class FansController {//5
    @Autowired
    FansService fansService;

    @Resource
    FansMapper fansMapper;

    @Autowired
    Ujwt ujwt;

//    关注
    @PostMapping("/add")
    public ResponseData tofans(@RequestBody String uid,@RequestHeader("token") String token){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String fansid=claims.getId();
        System.out.println(fansid);

//       判断两个id是否一致
        if(uid.equals(fansid)){
            return new ResponseData(null,false,"不能关注自己",204);
        }

//        首先先搜索是否存在关注的情况
        QueryWrapper<Fans> qw=new QueryWrapper<>();
        qw.eq("uid",uid);
        qw.eq("fansid",fansid);
        Fans f=fansService.getOne(qw);

        if(f==null){
            Fans fans=new Fans();
            fans.setId( UUID.randomUUID().toString().replaceAll("-", ""));
            fans.setUid(uid);
            fans.setFansid(fansid);
            Date date=new Date();
            java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
            fans.setCreatetime(timestamp);
            boolean i=fansService.addfan(fans);

            if(i){
                return new ResponseData(i,true,"关注成功",200);
            }else{
                return new ResponseData(i,false,"关注失败",201);
            }
        }
        return new ResponseData("已关注",false,"已关注",203);
    }

//    是否关注
    @GetMapping
    public ResponseData isfans(String uid,String fansid){
        QueryWrapper<Fans> qw=new QueryWrapper<>();
        qw.eq("uid",uid);
        qw.eq("fansid",fansid);
        Fans f=fansService.getOne(qw);
        if(f==null){
            return new ResponseData(1,true,"未关注",200);
        }else{
            return new ResponseData(2,true,"已关注",200);
        }

    }

//    列出某人的所有粉丝
    @GetMapping("/selectFansList")
    public ResponseData selallfans(String uid){

        MPJLambdaWrapper<Fans> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(User::getId,User::getUsername,User::getImg).innerJoin(User.class,User::getId,Fans::getFansid).eq(Fans::getUid,uid);
        List<User> UserList=fansMapper.selectJoinList(User.class,wrapper);
        List<FansUser> fansUserList=new ArrayList<>();
        for(User u:UserList){
            FansUser fs=new FansUser();
            fs.setImg(u.getImg());
            fs.setUid(u.getId());
            fs.setUsername(u.getUsername());
            fansUserList.add(fs);
        }

        return new ResponseData(fansUserList,true,"获取到该用户所有的粉丝列表",200);
    }


//    不在关注
    @DeleteMapping("/del")
    public ResponseData delfans(@RequestHeader("token") String token,@RequestParam("uid") String uid){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String fansid=claims.getId();
        System.out.println(fansid);

//       判断两个id是否一致
        if(uid.equals(fansid)){
            return new ResponseData(null,false,"不能关注自己",204);
        }

        QueryWrapper<Fans> qw=new QueryWrapper<>();
        qw.eq("uid",uid);
        qw.eq("fansid",fansid);
        Fans f=fansService.getOne(qw);
        if(f==null){
            return new ResponseData(null,false,"不存在关注关系",203);
        }else{
            boolean i=fansService.delfan(f);
            if(i){
                return new ResponseData(i,true,"已取消关注",200);
            }else{
                return new ResponseData(i,false,"取消关注失败，请联系我们",201);
            }
        }
    }

//    根据uid获取这个用户关注的博主
//    列出某人的所有粉丝
@GetMapping("/selectMyFansBloggers")
public ResponseData selectMyFansBloggers(String uid){

    MPJLambdaWrapper<Fans> wrapper = new MPJLambdaWrapper<>();
    wrapper.select(User::getId,User::getUsername,User::getImg).innerJoin(User.class,User::getId,Fans::getUid).eq(Fans::getFansid,uid);
    List<User> UserList=fansMapper.selectJoinList(User.class,wrapper);
    List<FansUser> fansUserList=new ArrayList<>();
    for(User u:UserList){
        FansUser fs=new FansUser();
        fs.setImg(u.getImg());
        fs.setUid(u.getId());
        fs.setUsername(u.getUsername());
        fansUserList.add(fs);
    }

    return new ResponseData(fansUserList,true,"获取到该用户所有的粉丝列表",200);
}


}
